package Connectivity;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Logger;

import util.MyLogging;
import ReadingData.ReadData;

public class ConnectionManager {

	private final static Logger LOGGER = new MyLogging()
			.getLogger(ConnectionManager.class.getName());
	private static Connection con = null;
	static String url;

	private ConnectionManager() {

	}

	public static Connection getConnection() {

		try {
			String original_path = new File("..").getCanonicalPath();
			String created_path=original_path+"\\server\\default\\conf\\jdbc.properties";
			//String created_path = original_path + "\\jdbc.properties";

			String url = ReadData.readData("url", created_path);
			String driver = ReadData.readData("driver", created_path);
			String user = ReadData.readData("user", created_path);
			String password = ReadData.readData("password", created_path);

			// String url = "jdbc:postgresql://localhost:5431/CRM";
			// assuming "DataSource" is your DataSource name

			// Class.forName("org.postgresql.Driver");

			Class.forName(driver);

			con = DriverManager.getConnection(url, user, password);

			// assuming your SQL Server's username is "username"
			// and password is "password"

			//System.out.println("succesfull conneciton");
			//LOGGER.info("successfull connection");
		} 
		catch (SQLException ex) 
		{
			System.out.println(ex);
			LOGGER.severe(ex.getMessage());	
			
		} 
		catch (ClassNotFoundException e) 
		{
			System.out.println(e);
			LOGGER.severe(e.getMessage());
		} 
		catch (IOException io) 
		{
			System.out.println(io);
			LOGGER.severe(io.getMessage());
		}

		return con;
	}
	
	public static void main(String[] args) {
		
		Connection con =getConnection();
	}
}